//
//  Sqlite.m
//  HomeLInk_HD
//
//  Created by hongbao.cui on 12-5-18.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "Sqlite.h"


@implementation Sqlite

#pragma mark collection
//测试成绩表
+(BOOL)creatPaperTable {
	NSString *path = FMPAPERSAVE_PATH;//[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Paper"];
	NSString *file = [path stringByAppendingPathComponent:kDatabaseName];
	//open database if exist, or create database
	if (sqlite3_open([file UTF8String], &database)!= SQLITE_OK) {
		sqlite3_close(database);
		return NO;
	}
	//create table
	char *erorMsg;
	NSString *createTableSQL = @"create table if not exists paperTable(paper_id integer, orderid integer, paper_title text,paper_time integer,paper_score integer,paper_auther text,exam_time text,exam_use_time integer,exam_score integer,remark text,qst_count integer,order_rule integer,finish_flag integer,exit_qst_id integer,soure text,type integer);";// primary key
	if (sqlite3_exec(database, [createTableSQL UTF8String], NULL, NULL, &erorMsg) != SQLITE_OK) {
		sqlite3_close(database);
		sqlite3_free(erorMsg);
		return NO;
	}
	return YES;
}

+(BOOL)addPaper:(NSDictionary *)dict{
	BOOL result = NO;
	if ([Sqlite creatPaperTable]) {
		NSString *memberId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
        NSString *paper_id = [dict objectForKey:@"paper_id"];
        NSString *orderid = [dict objectForKey:@"orderid"];
        NSString *paper_title = [dict objectForKey:@"paper_title"];
        NSString *paper_time = [dict objectForKey:@"paper_time"];
        NSString *paper_score = [dict objectForKey:@"paper_score"];
//        NSString *paper_auther = [dict objectForKey:@"paper_auther"];
        NSString *exam_time = [dict objectForKey:@"exam_time"];
//        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
//        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//        NSDate *exam_time = [dateFormatter dateFromString:exam_time_];
        FMSCHBLog(@"%@", exam_time);
//        [dateFormatter release];
        NSString *exam_use_time = [dict objectForKey:@"exam_use_time"];
        NSString *exam_score = [dict objectForKey:@"exam_score"];
        NSString *remark = [dict objectForKey:@"remark"];//存放答题记录
        NSString *qst_count = [dict objectForKey:@"qst_count"];
        NSString *order_rule = [dict objectForKey:@"order_rule"];
        NSString *finish_flag = [dict objectForKey:@"finish_flag"];
        NSString *exit_qst_id = [dict objectForKey:@"exit_qst_id"];
        NSString *soure = [dict objectForKey:@"soure"];
        NSString *type = [dict objectForKey:@"type"];
		NSString *updateSQL = [NSString stringWithFormat:@"insert into paperTable(paper_id,orderid,paper_title,paper_time,paper_score,paper_auther,exam_time,exam_use_time,exam_score,remark,qst_count,order_rule,finish_flag,exit_qst_id,soure,type) values('%d','%d','%@','%d','%d','%@','%@','%d','%d','%@','%d','%d','%d','%d','%@','%d');",[paper_id integerValue],[orderid integerValue],paper_title,[paper_time integerValue],[paper_score integerValue],memberId,exam_time,[exam_use_time integerValue],[exam_score integerValue],remark,[qst_count integerValue],[order_rule integerValue],[finish_flag integerValue],[exit_qst_id integerValue],soure,[type integerValue]];
		char *errorMsg;
		if (sqlite3_exec(database, [updateSQL UTF8String], NULL, NULL, &errorMsg) == SQLITE_OK) {
			sqlite3_free(errorMsg);
			result = YES;
		}
		else {
		}
		sqlite3_close(database);
	}
	return result;
}
//或得结果试卷
+(NSMutableDictionary *)selectResultPaper{
	if (![Sqlite creatPaperTable]) {
		return nil;
	}
    NSString *userName = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
	NSString *selectSQL = [NSString stringWithFormat:@"select * from paperTable where finish_flag = '%d' and paper_auther = '%@';",1,userName];
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:1];
	sqlite3_stmt *statement;
	if (sqlite3_prepare_v2(database, [selectSQL UTF8String], -1, &statement, nil) == SQLITE_OK) {
		while (sqlite3_step(statement) == SQLITE_ROW) {
			int paper_id = (int)sqlite3_column_int(statement, 0);
            int orderid = (int)sqlite3_column_int(statement, 1);
			const char *paper_title = (char*)sqlite3_column_text(statement, 2);
			int paper_time = (int)sqlite3_column_int(statement, 3);
			int paper_score = (int)sqlite3_column_int(statement, 4);
			const char *paper_auther = (char*)sqlite3_column_text(statement, 5);
			const char *exam_time = (char*)sqlite3_column_text(statement, 6);//time
			int exam_use_time = (int )sqlite3_column_int(statement, 7);
			int exam_score = (int)sqlite3_column_int(statement, 8);
			const char *remark = (char*)sqlite3_column_text(statement, 9);
			int qst_count = (int)sqlite3_column_int(statement, 10);
			int order_rule = (int )sqlite3_column_int(statement, 11);
			int finish_flag = (int )sqlite3_column_int(statement, 12);
			int exit_qst_id = (int )sqlite3_column_int(statement, 13);
			const char *soure = (char*)sqlite3_column_text(statement, 14);
			int type = (int )sqlite3_column_int(statement, 15);
            [dict setObject:[NSString stringWithFormat:@"%d",paper_id] forKey:@"paper_id"];
            [dict setObject:[NSString stringWithFormat:@"%d",orderid] forKey:@"orderid"];
            [dict setObject:[NSString stringWithUTF8String:paper_title] forKey:@"paper_title"];
            [dict setObject:[NSString stringWithFormat:@"%d",paper_time] forKey:@"paper_time"];
            [dict setObject:[NSString stringWithFormat:@"%d",paper_score] forKey:@"paper_score"];
            [dict setObject:[NSString stringWithUTF8String:paper_auther] forKey:@"paper_auther"];
            [dict setObject:[NSString stringWithUTF8String:exam_time] forKey:@"exam_time"];
            [dict setObject:[NSString stringWithFormat:@"%d",exam_use_time] forKey:@"exam_use_time"];
            [dict setObject:[NSString stringWithFormat:@"%d",exam_score] forKey:@"exam_score"];
            [dict setObject:[NSString stringWithUTF8String:remark] forKey:@"remark"];
            [dict setObject:[NSString stringWithFormat:@"%d",qst_count] forKey:@"qst_count"];
            [dict setObject:[NSString stringWithFormat:@"%d",order_rule] forKey:@"order_rule"];
            [dict setObject:[NSString stringWithFormat:@"%d",finish_flag] forKey:@"finish_flag"];
            [dict setObject:[NSString stringWithFormat:@"%d",exit_qst_id] forKey:@"exit_qst_id"];
            [dict setObject:[NSString stringWithUTF8String:soure] forKey:@"soure"];
            [dict setObject:[NSString stringWithFormat:@"%d",type] forKey:@"type"];
		}
	}
	sqlite3_finalize(statement);
	sqlite3_close(database);
	return dict;
}
//根据flag值判断试卷是否完成
+(NSString *)finish_flag:(NSString *)paper_id{
    if (![Sqlite creatPaperTable]) {
		return nil;
	}
    NSString *userName = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
	NSString *selectSQL = [NSString stringWithFormat:@"select * from paperTable where paper_auther = '%@' and paper_id = '%@';",userName,paper_id];
	sqlite3_stmt *statement;
    NSString *finishStr = nil;
	if (sqlite3_prepare_v2(database, [selectSQL UTF8String], -1, &statement, nil) == SQLITE_OK) {
		while (sqlite3_step(statement) == SQLITE_ROW) {
			int finish_flag = (int )sqlite3_column_int(statement, 12);
            finishStr =[NSString stringWithFormat:@"%d",finish_flag];

		}
	}
	sqlite3_finalize(statement);
	sqlite3_close(database);
    return finishStr;
}
//所有测试成绩的表格
+(NSMutableArray *)selectAllPaper:(NSInteger )index{
	if (![Sqlite creatPaperTable]) {
		return nil;
	}
    NSString *userName = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
	NSString *selectSQL = [NSString stringWithFormat:@"select * from paperTable where paper_auther = '%@' and finish_flag = '%ld';",userName,(long)index];
    NSMutableArray *dataArray = [NSMutableArray arrayWithCapacity:1];
	sqlite3_stmt *statement;
	if (sqlite3_prepare_v2(database, [selectSQL UTF8String], -1, &statement, nil) == SQLITE_OK) {
		while (sqlite3_step(statement) == SQLITE_ROW) {
            NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:1];
			int paper_id = (int)sqlite3_column_int(statement, 0);
            int orderid = (int)sqlite3_column_int(statement, 1);
			const char *paper_title = (char*)sqlite3_column_text(statement, 2);
			int paper_time = (int)sqlite3_column_int(statement, 3);
			int paper_score = (int)sqlite3_column_int(statement, 4);
			const char *paper_auther = (char*)sqlite3_column_text(statement, 5);
			const char *exam_time = (char*)sqlite3_column_text(statement, 6);//time
			int exam_use_time = (int )sqlite3_column_int(statement, 7);
			int exam_score = (int)sqlite3_column_int(statement, 8);
			const char *remark = (char*)sqlite3_column_text(statement, 9);
			int qst_count = (int)sqlite3_column_int(statement, 10);
			int order_rule = (int )sqlite3_column_int(statement, 11);
			int finish_flag = (int )sqlite3_column_int(statement, 12);
			int exit_qst_id = (int )sqlite3_column_int(statement, 13);
			const char *soure = (char*)sqlite3_column_text(statement, 14);
			int type = (int )sqlite3_column_int(statement, 15);
            [dict setObject:[NSString stringWithFormat:@"%d",paper_id] forKey:@"paper_id"];
            [dict setObject:[NSString stringWithFormat:@"%d",orderid] forKey:@"orderid"];
            [dict setObject:[NSString stringWithUTF8String:paper_title] forKey:@"paper_title"];
            [dict setObject:[NSString stringWithFormat:@"%d",paper_time] forKey:@"paper_time"];
            [dict setObject:[NSString stringWithFormat:@"%d",paper_score] forKey:@"paper_score"];
            [dict setObject:[NSString stringWithUTF8String:paper_auther] forKey:@"paper_auther"];
            [dict setObject:[NSString stringWithUTF8String:exam_time] forKey:@"exam_time"];
            [dict setObject:[NSString stringWithFormat:@"%d",exam_use_time] forKey:@"exam_use_time"];
            [dict setObject:[NSString stringWithFormat:@"%d",exam_score] forKey:@"exam_score"];
            [dict setObject:[NSString stringWithUTF8String:remark] forKey:@"remark"];
            [dict setObject:[NSString stringWithFormat:@"%d",qst_count] forKey:@"qst_count"];
            [dict setObject:[NSString stringWithFormat:@"%d",order_rule] forKey:@"order_rule"];
            [dict setObject:[NSString stringWithFormat:@"%d",finish_flag] forKey:@"finish_flag"];
            [dict setObject:[NSString stringWithFormat:@"%d",exit_qst_id] forKey:@"exit_qst_id"];
            [dict setObject:[NSString stringWithUTF8String:soure] forKey:@"soure"];
            [dict setObject:[NSString stringWithFormat:@"%d",type] forKey:@"type"];
			[dataArray addObject:dict];
		}
	}
	sqlite3_finalize(statement);
	sqlite3_close(database);
	return dataArray;
}
+(BOOL)deletePaperIdAt:(NSString*)paper_Id {
	BOOL result = NO;
	if ([Sqlite creatPaperTable]) {
		NSString *userName = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
		NSString *updateSQL = [NSString stringWithFormat:@"delete from paperTable where paper_auther = '%@' and paper_id = '%ld';",userName,(long)[paper_Id integerValue]];
		char *errorMsg;
		if (sqlite3_exec(database, [updateSQL UTF8String], NULL, NULL, &errorMsg) == SQLITE_OK) {
			sqlite3_free(errorMsg);
			result = YES;
		}
		else {
		}
		sqlite3_close(database);
	}
	return result;
}
+(BOOL)deleteAllPaper{
	BOOL result = NO;
	if ([Sqlite creatPaperTable]) {
		NSString *userName = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
		NSString *updateSQL = [NSString stringWithFormat:@"delete from paperTable where paper_auther = '%@';",userName];
		char *errorMsg;
		if (sqlite3_exec(database, [updateSQL UTF8String], NULL, NULL, &errorMsg) == SQLITE_OK) {
			sqlite3_free(errorMsg);
			result = YES;
		}
		else {
		}
		sqlite3_close(database);
	}
	return result;
}

#pragma mark -
#pragma mark 测试后的试题
//
+(BOOL)creatQuestionTable {
	NSString *path = FMPAPERSAVE_PATH;//[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Paper"];
	NSString *file = [path stringByAppendingPathComponent:kDatabaseName];
	//open database if exist, or create database
	if (sqlite3_open([file UTF8String], &database)!= SQLITE_OK) {
		sqlite3_close(database);
		return NO;
	}
	//create table
	char *erorMsg;// primary key
	NSString *createTableSQL = @"create table if not exists questionTable(qst_id integer,paper_id integer,orderid integer,category_id integer,category_name text,qtype integer,paper_title text,scroe integer,content text,option_a text,option_b text,option_c text,option_d text,option_e text,option_f text,answer text,analysis text,parent_id text,user_answer text,use_error_count integer,use_right_count integer,favourite_flag integer,user_test_sign integer,has_sub integer,difficult_id integer,difficult_name text,title_id integer,qst_no integer,tq_id integer,author_id text,exam_time text);";
    
	if (sqlite3_exec(database, [createTableSQL UTF8String], NULL, NULL, &erorMsg) != SQLITE_OK) {
		sqlite3_close(database);
		sqlite3_free(erorMsg);
		return NO;
	}
	return YES;
}
//
+(BOOL)insertQuestion:(NSMutableDictionary *)dict{
	BOOL result = NO;
	if ([Sqlite creatQuestionTable]) {
        NSString *author_id = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
        NSString *qst_id = [dict objectForKey:@"qst_id"];
        NSString *paper_id = [dict objectForKey:@"paper_id"];
        NSString *orderid = [dict objectForKey:@"orderid"];
        NSString *category_id = [dict objectForKey:@"category_id"];
        NSString *category_name = [dict objectForKey:@"category_name"];
        NSString *qtype = [dict objectForKey:@"qtype"];
        NSString *qtype_name = [dict objectForKey:@"paper_title"];
        NSString *scroe = [dict objectForKey:@"scroe"];
        NSString *content = [dict objectForKey:@"content"];
        NSArray *array = [dict objectForKey:@"options"];
        NSMutableString *optionsString = [NSMutableString stringWithString:@""];
        //        NSArray *keyArray = [NSArray arrayWithObjects:@"option_a",@"option_b",@"option_c",@"option_d",@"option_e",@"option_f",@"option_g",@"option_h",@"option_i",@"option_j",@"option_k", nil];
        for (NSInteger i=0;i<[array count];i++) {
            [optionsString appendFormat:@"%@$",[array objectAtIndex:i]];
            //            [dict setObject:[array objectAtIndex:i] forKey:[keyArray objectAtIndex:i]];
        }
        //        NSString *option_a = [dict objectForKey:@"option_a"];
        NSString *option_a = optionsString;
//        NSString *option_a = [dict objectForKey:@"option_a"];
        NSString *option_b = [dict objectForKey:@"option_b"];
        NSString *option_c = [dict objectForKey:@"option_c"];
        NSString *option_d = [dict objectForKey:@"option_d"];
        NSString *option_e = [dict objectForKey:@"option_e"];
        NSString *option_f = [dict objectForKey:@"option_f"];
        NSString *answer = [dict objectForKey:@"answer"];
        NSString *analysis = [dict objectForKey:@"analysis"];
        NSString *parent_id = [dict objectForKey:@"parent_id"];
        NSString *user_answer = [dict objectForKey:@"user_answer"];
        NSString *use_error_count = [dict objectForKey:@"user_error_count"];
        NSString *use_right_count = [dict objectForKey:@"user_right_count"];
        NSString *favourite_flag = [dict objectForKey:@"favourite_flag"];
        NSString *user_test_sign = [dict objectForKey:@"user_test_sign"];
        NSString *has_sub = [dict objectForKey:@"has_sub"];
        NSString *difficult_id = [dict objectForKey:@"difficult_id"];
        NSString *difficult_name = [dict objectForKey:@"difficult_name"];
        NSString *title_id = [dict objectForKey:@"title_id"];
        NSString *qst_no = [dict objectForKey:@"qst_no"];
        NSString *tq_id = [dict objectForKey:@"tq_id"];
        NSString *exam_time = [dict objectForKey:@"exam_time"];
		NSString *updateSQL = [NSString stringWithFormat:@"insert into questionTable(paper_id,orderid,category_id,category_name,qtype,paper_title,scroe,content,option_a,option_b,option_c,option_d,option_e,option_f,answer,analysis,parent_id,user_answer,use_error_count,use_right_count,favourite_flag,user_test_sign,has_sub,difficult_id,difficult_name,title_id,qst_no,tq_id,author_id,exam_time) values('%ld','%ld','%ld','%@','%ld','%@','%ld','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%ld','%ld','%ld','%ld','%ld','%ld','%@','%ld','%ld','%ld','%@','%@');",(long)[paper_id integerValue],(long)[orderid integerValue],(long)[category_id integerValue],category_name,(long)[qtype integerValue],qtype_name,(long)[scroe integerValue],content,option_a,option_b,option_c,option_d,option_e,option_f,answer,analysis,parent_id,user_answer,(long)[use_error_count integerValue],(long)[use_right_count integerValue],(long)[favourite_flag integerValue],(long)[user_test_sign integerValue],(long)[has_sub integerValue],(long)[difficult_id integerValue],difficult_name,(long)[title_id integerValue],(long)[qst_no integerValue],(long)[tq_id integerValue],author_id,exam_time];
		char *errorMsg;
		if (sqlite3_exec(database, [updateSQL UTF8String], NULL, NULL, &errorMsg) == SQLITE_OK) {
			sqlite3_free(errorMsg);
			result = YES;
		}
		else {
		}
		sqlite3_close(database);
	}
	return result;
}
//
+(NSMutableArray *)selectAllQuestion:(NSString *)paper_id exam_time:(NSString *)exam_time{
    if (![Sqlite creatQuestionTable]) {
        return nil;
    }
    NSString *memberId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
    NSString *selectSQL = [NSString stringWithFormat:@"select * from questionTable where paper_id = '%@' and exam_time = '%@' and author_id = '%@';",paper_id,exam_time,memberId];
    sqlite3_stmt *statement;
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:1];
    BOOL hasFind = NO;
    if (sqlite3_prepare_v2(database, [selectSQL UTF8String], -1, &statement, nil) == SQLITE_OK) {
        while (sqlite3_step(statement) == SQLITE_ROW) {
            NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:1];
            int qstId = (int)sqlite3_column_int(statement, 0);
            int paper_id = (int)sqlite3_column_int(statement, 1);
            int orderid = (int)sqlite3_column_int(statement, 2);
            int category_id = (int)sqlite3_column_int(statement, 3);
            const char *category_name = (char*)sqlite3_column_text(statement,4);
            int qtype = (int)sqlite3_column_int(statement, 5);
            const char *qtype_name = (char*)sqlite3_column_text(statement, 6);
            int score = (int )sqlite3_column_int(statement, 7);
            const char *content = (char*)sqlite3_column_text(statement, 8);
            const char *option_a = (char*)sqlite3_column_text(statement, 9);
//            const char *option_b = (char*)sqlite3_column_text(statement, 10);
//            const char *option_c = (char*)sqlite3_column_text(statement,11);
//            const char *option_d = (char*)sqlite3_column_text(statement, 12);
//            const char *option_e = (char*)sqlite3_column_text(statement,13);
//            const char *option_f = (char*)sqlite3_column_text(statement, 14);
            const char *answer = (char*)sqlite3_column_text(statement, 15);
            const char *analysis = (char*)sqlite3_column_text(statement, 16);
            const char * parent_id = (char*)sqlite3_column_text(statement, 17);
            const char *user_answer = (char*)sqlite3_column_text(statement, 18);
            int user_error_count = (int )sqlite3_column_int(statement, 19);
            int user_right_count = (int )sqlite3_column_int(statement, 20);
            int favourite_flag = (int )sqlite3_column_int(statement, 21);
            int user_test_sign = (int )sqlite3_column_int(statement, 22);
            int has_sub = (int )sqlite3_column_int(statement, 23);
            int difficult_id = (int )sqlite3_column_int(statement, 24);
            const char *difficult_name = (char*)sqlite3_column_text(statement, 25);
            int title_id = (int )sqlite3_column_int(statement, 26);
            int qst_no = (int )sqlite3_column_int(statement, 27);
            int tq_id = (int )sqlite3_column_int(statement, 28);
            const char *author_id = (char*)sqlite3_column_text(statement, 29);
            const char *exam_time = (char*)sqlite3_column_text(statement, 30);

            [dict setObject:[NSString stringWithFormat:@"%d",qstId] forKey:@"qstId"];
            [dict setObject:[NSString stringWithFormat:@"%d",paper_id] forKey:@"paper_id"];
            [dict setObject:[NSString stringWithFormat:@"%d",orderid] forKey:@"orderid"];
            [dict setObject:[NSString stringWithFormat:@"%d",category_id] forKey:@"category_id"];
            [dict setObject:[NSString stringWithUTF8String:category_name] forKey:@"category_name"];
            [dict setObject:[NSString stringWithFormat:@"%d",qtype] forKey:@"qtype"];
            [dict setObject:[NSString stringWithUTF8String:qtype_name] forKey:@"paper_title"];
            [dict setObject:[NSString stringWithFormat:@"%d",score] forKey:@"score"];
            [dict setObject:[NSString stringWithUTF8String:content] forKey:@"content"];
//            NSMutableArray *optionArray = [NSMutableArray arrayWithCapacity:1];
            NSString *a = [NSString stringWithUTF8String:option_a];
            NSArray *arrayO = [a componentsSeparatedByString:@"$"];
            NSMutableArray *optionArray = [NSMutableArray arrayWithArray:arrayO];
            [optionArray removeLastObject];
//            if (a&&![a isEqualToString:@"(null)"]) {
//                [optionArray addObject:a];
//            }
//            NSString *b = [NSString stringWithUTF8String:option_b];
//            if (b&&![b isEqualToString:@"(null)"]) {
//                [optionArray addObject:b];
//            }
//            NSString *c = [NSString stringWithUTF8String:option_c];
//            if (c&&![c isEqualToString:@"(null)"]) {
//                [optionArray addObject:c];
//            }
//            NSString *d = [NSString stringWithUTF8String:option_d];
//            if (d&&![d isEqualToString:@"(null)"]) {
//                [optionArray addObject:d];
//            }
//            NSString *e = [NSString stringWithUTF8String:option_e];
//            if (e&&![e isEqualToString:@"(null)"]) {
//                [optionArray addObject:e];
//            }
//            NSString *f = [NSString stringWithUTF8String:option_f];
//            if (f&&![f isEqualToString:@"(null)"]) {
//                [optionArray addObject:f];
//            }
            [dict setObject:optionArray forKey:@"options"];
            [dict setObject:[NSString stringWithUTF8String:answer] forKey:@"answer"];
            [dict setObject:[NSString stringWithUTF8String:analysis] forKey:@"analysis"];
            [dict setObject:[NSString stringWithUTF8String:parent_id] forKey:@"parent_id"];
            [dict setObject:[NSString stringWithUTF8String:user_answer] forKey:@"user_answer"];
            [dict setObject:[NSString stringWithFormat:@"%d",user_error_count] forKey:@"user_error_count"];
            [dict setObject:[NSString stringWithFormat:@"%d",user_right_count] forKey:@"user_right_count"];
            [dict setObject:[NSString stringWithFormat:@"%d",favourite_flag] forKey:@"favourite_flag"];
            [dict setObject:[NSString stringWithFormat:@"%d",user_test_sign] forKey:@"user_test_sign"];
            [dict setObject:[NSString stringWithFormat:@"%d",has_sub] forKey:@"has_sub"];
            [dict setObject:[NSString stringWithFormat:@"%d",difficult_id] forKey:@"difficult_id"];
            [dict setObject:[NSString stringWithUTF8String:difficult_name] forKey:@"difficult_name"];
            [dict setObject:[NSString stringWithFormat:@"%d",title_id] forKey:@"title_id"];
            [dict setObject:[NSString stringWithFormat:@"%d",qst_no] forKey:@"qst_no"];
            [dict setObject:[NSString stringWithFormat:@"%d",tq_id] forKey:@"tq_id"];
            [dict setObject:[NSString stringWithUTF8String:author_id] forKey:@"author_id"];
            [dict setObject:[NSString stringWithUTF8String:exam_time] forKey:@"exam_time"];
            [array addObject:dict];
            hasFind = YES;
        }
    }
    if (!hasFind) {
        array = nil;
    }
    sqlite3_finalize(statement);
    sqlite3_close(database);
    return array;
}
+(BOOL)deleteAllQuestion:(NSString *)paperId{
    BOOL result = NO;
    if ([Sqlite creatQuestionTable]) {
        NSString *userName = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
        NSString *updateSQL = [NSString stringWithFormat:@"delete from questionTable where paper_id = '%@' and paper_auther = '%@';",paperId,userName];
        char *errorMsg;
        if (sqlite3_exec(database, [updateSQL UTF8String], NULL, NULL, &errorMsg) == SQLITE_OK) {
            sqlite3_free(errorMsg);
            result = YES;
        }
        else {
        }
        sqlite3_close(database);
    }
    return result;
}
#pragma mark-
#pragma mark-收藏方法
//收藏相关方法
+(BOOL)updateCollection:(NSString*)favourite_flag  orderId:(NSString *)qstId paperId:(NSString *)paperId{
    BOOL isResult;
	if(![Sqlite creatTestQuestionTable]) {
        isResult = NO;
		return isResult;
	}
    NSString *author_id = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
    NSString *updateSQL = [NSString stringWithFormat:@"update testQuestionTable set favourite_flag = '%@' where qstId='%@' and paper_id = '%@' and author_id ='%@';", favourite_flag,qstId,paperId,author_id];
	char *errorMsg;
	if (sqlite3_exec(database, [updateSQL UTF8String], NULL, NULL, &errorMsg) == SQLITE_OK) {
		sqlite3_free(errorMsg);
        isResult = YES;
	}
	else {
		FMSCHBLog(@"updateData fail error:%s",errorMsg);
        isResult = NO;
		return isResult;
	}
	sqlite3_close(database);
	return isResult;
}
//没有批改过的试卷不能查看答案，只有批改过的试卷能查看答案
+(BOOL)updateCollection:(NSString*)favourite_flag paperId:(NSString *)paperId{
    BOOL isResult;
    if(![Sqlite creatTestQuestionTable]) {
        isResult = NO;
        return isResult;
    }
    NSString *author_id = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
    NSString *updateSQL = [NSString stringWithFormat:@"update testQuestionTable set collection_finish_flag = '%@' where favourite_flag = '1' and paper_id = '%@' and author_id ='%@';", favourite_flag,paperId,author_id];
    char *errorMsg;
    if (sqlite3_exec(database, [updateSQL UTF8String], NULL, NULL, &errorMsg) == SQLITE_OK) {
        sqlite3_free(errorMsg);
        isResult = YES;
    }
    else {
        FMSCHBLog(@"updateData fail error:%s",errorMsg);
        isResult = NO;
        return isResult;
    }
    sqlite3_close(database);
    return isResult;
}
//测试收藏的方法
+(NSMutableArray *)selectTestCollectionQuestion{
    if (![Sqlite creatTestQuestionTable]) {
		return nil;
	}
    NSMutableArray  *dataArray = [NSMutableArray arrayWithCapacity:1];
    
    NSString *memberId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
	NSString *selectSQL = [NSString stringWithFormat:@"select * from testQuestionTable where favourite_flag ='1' and author_id = '%@' group by paper_id;",memberId];
	sqlite3_stmt *statement;
	BOOL hasFind = NO;
	if (sqlite3_prepare_v2(database, [selectSQL UTF8String], -1, &statement, nil) == SQLITE_OK) {
		while (sqlite3_step(statement) == SQLITE_ROW) {
            NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:1];
			int qst_id = (int)sqlite3_column_int(statement, 0);
             const char *paper_id = (char*)sqlite3_column_text(statement, 1);
            int orderid = (int)sqlite3_column_int(statement, 2);
			int category_id = (int)sqlite3_column_int(statement, 3);
            const char *category_name = (char*)sqlite3_column_text(statement,4);
			int qtype = (int)sqlite3_column_int(statement, 5);
			const char *qtype_name = (char*)sqlite3_column_text(statement, 6);
			int score = (int )sqlite3_column_int(statement, 7);
			const char *content = (char*)sqlite3_column_text(statement, 8);
			const char *option_a = (char*)sqlite3_column_text(statement, 9);
//            const char *option_b = (char*)sqlite3_column_text(statement, 10);
//			const char *option_c = (char*)sqlite3_column_text(statement,11);
//			const char *option_d = (char*)sqlite3_column_text(statement, 12);
//			const char *option_e = (char*)sqlite3_column_text(statement,13);
//			const char *option_f = (char*)sqlite3_column_text(statement, 14);
			const char *answer = (char*)sqlite3_column_text(statement, 15);
			const char *analysis = (char*)sqlite3_column_text(statement, 16);
			const char * parent_id = (char*)sqlite3_column_text(statement, 17);
            const char *user_answer = (char*)sqlite3_column_text(statement, 18);
			int user_error_count = (int )sqlite3_column_int(statement, 19);
			int user_right_count = (int )sqlite3_column_int(statement, 20);
			int favourite_flag = (int )sqlite3_column_int(statement, 21);
			int user_test_sign = (int )sqlite3_column_int(statement, 22);
            int has_sub = (int )sqlite3_column_int(statement, 23);
			int difficult_id = (int )sqlite3_column_int(statement, 24);
			const char *difficult_name = (char*)sqlite3_column_text(statement, 25);
			int titile_id = (int )sqlite3_column_int(statement, 26);
			int qst_no = (int )sqlite3_column_int(statement, 27);
			int tq_id = (int )sqlite3_column_int(statement, 28);
			const char *author_id = (char*)sqlite3_column_text(statement, 29);
            const char *paper_title = (char*)sqlite3_column_text(statement, 30);
            const char *paper_score = (char*)sqlite3_column_text(statement, 31);
            const char *paper_time = (char*)sqlite3_column_text(statement, 32);
            const char *myqtype = (char*)sqlite3_column_text(statement, 33);
            const char *collection_finish_flag = (char*)sqlite3_column_text(statement, 34);
            const char *userAnswerImage = (char*)sqlite3_column_text(statement, 35);
            
            [dict setObject:[NSString stringWithFormat:@"%d",qst_id] forKey:@"qst_id"];
            [dict setObject:[NSString stringWithUTF8String:paper_id] forKey:@"paper_id"];
            [dict setObject:[NSString stringWithFormat:@"%d",orderid] forKey:@"orderid"];
            [dict setObject:[NSString stringWithFormat:@"%d",category_id] forKey:@"category_id"];
            [dict setObject:[NSString stringWithUTF8String:category_name] forKey:@"category_name"];
            [dict setObject:[NSString stringWithFormat:@"%d",qtype] forKey:@"qtype"];
            [dict setObject:[NSString stringWithUTF8String:qtype_name] forKey:@"qtype_name"];
            [dict setObject:[NSString stringWithFormat:@"%d",score] forKey:@"score"];
            [dict setObject:[NSString stringWithUTF8String:content] forKey:@"content"];
//            NSMutableArray *optionArray = [NSMutableArray arrayWithCapacity:1];
            NSString *a = [NSString stringWithUTF8String:option_a];
            NSArray *array1 = [a componentsSeparatedByString:@"$"];
            NSMutableArray *optionArray = [NSMutableArray arrayWithArray:array1];
            [optionArray removeLastObject];
//            if (a&&![a isEqualToString:@"(null)"]) {
//                [optionArray addObject:a];
//            }
//            NSString *b = [NSString stringWithUTF8String:option_b];
//            if (b&&![b isEqualToString:@"(null)"]) {
//                [optionArray addObject:b];
//            }
//            NSString *c = [NSString stringWithUTF8String:option_c];
//            if (c&&![c isEqualToString:@"(null)"]) {
//                [optionArray addObject:c];
//            }
//            NSString *d = [NSString stringWithUTF8String:option_d];
//            if (d&&![d isEqualToString:@"(null)"]) {
//                [optionArray addObject:d];
//            }
//            NSString *e = [NSString stringWithUTF8String:option_e];
//            if (e&&![e isEqualToString:@"(null)"]) {
//                [optionArray addObject:e];
//            }
//            NSString *f = [NSString stringWithUTF8String:option_f];
//            if (f&&![f isEqualToString:@"(null)"]) {
//                [optionArray addObject:f];
//            }
            [dict setObject:optionArray forKey:@"options"];
            [dict setObject:[NSString stringWithUTF8String:answer] forKey:@"answer"];
            [dict setObject:[NSString stringWithUTF8String:analysis] forKey:@"analysis"];
            [dict setObject:[NSString stringWithUTF8String:parent_id] forKey:@"parent_id"];
            [dict setObject:[NSString stringWithUTF8String:user_answer] forKey:@"user_answer"];
            [dict setObject:[NSString stringWithFormat:@"%d",user_error_count] forKey:@"user_error_count"];
            [dict setObject:[NSString stringWithFormat:@"%d",user_right_count] forKey:@"user_right_count"];
            [dict setObject:[NSString stringWithFormat:@"%d",favourite_flag] forKey:@"favourite_flag"];
            [dict setObject:[NSString stringWithFormat:@"%d",user_test_sign] forKey:@"user_test_sign"];
            if (has_sub == 1) {
                NSMutableArray *array = [self selectGetSubQuestion:[NSString stringWithFormat:@"%d",tq_id] paperId:[NSString stringWithFormat:@"%d",paper_id]];
                NSArray *options = [NSArray arrayWithObjects:@"option_a",@"option_b",@"option_c",@"option_d",@"option_e",@"option_f",@"option_g", nil];
                for (NSInteger i=0; i<[array count]-1; i++) {
                    NSString *option = [array objectAtIndex:i];
                    [dict setObject:option forKey:[options objectAtIndex:i]];
                }
                NSDictionary *lastDict = [array lastObject];
                [dict setObject:[lastDict objectForKey:@"analysis"] forKey:@"analysis"];
            }
            [dict setObject:[NSString stringWithFormat:@"%d",has_sub] forKey:@"has_sub"];
            [dict setObject:[NSString stringWithFormat:@"%d",difficult_id] forKey:@"difficult_id"];
            [dict setObject:[NSString stringWithUTF8String:difficult_name] forKey:@"difficult_name"];
            [dict setObject:[NSString stringWithFormat:@"%d",titile_id] forKey:@"titile_id"];
            [dict setObject:[NSString stringWithFormat:@"%d",qst_no] forKey:@"qst_no"];
            [dict setObject:[NSString stringWithFormat:@"%d",tq_id] forKey:@"tq_id"];
            [dict setObject:[NSString stringWithUTF8String:author_id] forKey:@"author_id"];
            [dict setObject:[NSString stringWithUTF8String:paper_title] forKey:@"paper_title"];
            [dict setObject:[NSString stringWithUTF8String:paper_score] forKey:@"paper_score"];
            [dict setObject:[NSString stringWithUTF8String:paper_time] forKey:@"paper_time"];
            [dict setObject:[NSString stringWithUTF8String:paper_score] forKey:@"paper_score"];
            [dict setObject:[NSString stringWithUTF8String:paper_time] forKey:@"paper_time"];
            [dict setObject:[NSString stringWithUTF8String:myqtype] forKey:@"myqtype"];
            [dict setObject:[NSString stringWithUTF8String:collection_finish_flag] forKey:@"collection_finish_flag"];
            [dict setObject:[NSString stringWithUTF8String:userAnswerImage] forKey:@"userAnswerImage"];
            [dataArray addObject:dict];
			hasFind = YES;
		}
	}
    
	sqlite3_finalize(statement);
	sqlite3_close(database);
	return dataArray;
}
+(NSMutableArray *)selectCollectionQuestion{
    if (![Sqlite creatQuestionTable]) {
		return nil;
	}
    NSMutableArray *dataArray = [NSMutableArray arrayWithCapacity:1];
    NSString *memberId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
    NSString *selectSQL = [NSString stringWithFormat:@"select * from questionTable group by paper_title"];
	sqlite3_stmt *statement;
    if (sqlite3_prepare_v2(database, [selectSQL UTF8String], -1, &statement, nil) == SQLITE_OK) {
		while (sqlite3_step(statement) == SQLITE_ROW) {
        NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:1];
			int qst_id = (int)sqlite3_column_int(statement, 0);
            const char *paper_id = (char*)sqlite3_column_text(statement, 1);
            int orderid = (int)sqlite3_column_int(statement, 2);
			int category_id = (int)sqlite3_column_int(statement, 3);
            const char *category_name = (char*)sqlite3_column_text(statement,4);
			int qtype = (int)sqlite3_column_int(statement, 5);
			const char *qtype_name = (char*)sqlite3_column_text(statement, 6);
			int score = (int )sqlite3_column_int(statement, 7);
			const char *content = (char*)sqlite3_column_text(statement, 8);
			const char *option_a = (char*)sqlite3_column_text(statement, 9);
//            const char *option_b = (char*)sqlite3_column_text(statement, 10);
//			const char *option_c = (char*)sqlite3_column_text(statement,11);
//			const char *option_d = (char*)sqlite3_column_text(statement, 12);
//			const char *option_e = (char*)sqlite3_column_text(statement,13);
//			const char *option_f = (char*)sqlite3_column_text(statement, 14);
			const char *answer = (char*)sqlite3_column_text(statement, 15);
			const char *analysis = (char*)sqlite3_column_text(statement, 16);
			const char * parent_id = (char*)sqlite3_column_text(statement, 17);
            const char *user_answer = (char*)sqlite3_column_text(statement, 18);
			int user_error_count = (int )sqlite3_column_int(statement, 19);
			int user_right_count = (int )sqlite3_column_int(statement, 20);
			int favourite_flag = (int )sqlite3_column_int(statement, 21);
			int user_test_sign = (int )sqlite3_column_int(statement, 22);
            int has_sub = (int )sqlite3_column_int(statement, 23);
			int difficult_id = (int )sqlite3_column_int(statement, 24);
			const char *difficult_name = (char*)sqlite3_column_text(statement, 25);
			int titile_id = (int )sqlite3_column_int(statement, 26);
			int qst_no = (int )sqlite3_column_int(statement, 27);
			int tq_id = (int )sqlite3_column_int(statement, 28);
			const char *author_id = (char*)sqlite3_column_text(statement, 29);
            
            [dict setObject:[NSString stringWithFormat:@"%d",qst_id] forKey:@"qst_id"];
            [dict setObject:[NSString stringWithUTF8String:paper_id] forKey:@"paper_id"];
            [dict setObject:[NSString stringWithFormat:@"%d",orderid] forKey:@"orderid"];
            [dict setObject:[NSString stringWithFormat:@"%d",category_id] forKey:@"category_id"];
            [dict setObject:[NSString stringWithUTF8String:category_name] forKey:@"category_name"];
            [dict setObject:[NSString stringWithFormat:@"%d",qtype] forKey:@"qtype"];
            [dict setObject:[NSString stringWithUTF8String:qtype_name] forKey:@"paper_title"];
            [dict setObject:[NSString stringWithFormat:@"%d",score] forKey:@"score"];
            [dict setObject:[NSString stringWithUTF8String:content] forKey:@"content"];
//            NSMutableArray *optionArray = [NSMutableArray arrayWithCapacity:1];
            NSString *a = [NSString stringWithUTF8String:option_a];
            NSArray *array0 = [a componentsSeparatedByString:@"$"];
            NSMutableArray *optionArray = [NSMutableArray arrayWithArray:array0];
            [optionArray removeLastObject];
//            if (a&&![a isEqualToString:@"(null)"]) {
//                [optionArray addObject:a];
//            }
//            NSString *b = [NSString stringWithUTF8String:option_b];
//            if (b&&![b isEqualToString:@"(null)"]) {
//                [optionArray addObject:b];
//            }
//            NSString *c = [NSString stringWithUTF8String:option_c];
//            if (c&&![c isEqualToString:@"(null)"]) {
//                [optionArray addObject:c];
//            }
//            NSString *d = [NSString stringWithUTF8String:option_d];
//            if (d&&![d isEqualToString:@"(null)"]) {
//                [optionArray addObject:d];
//            }
//            NSString *e = [NSString stringWithUTF8String:option_e];
//            if (e&&![e isEqualToString:@"(null)"]) {
//                [optionArray addObject:e];
//            }
//            NSString *f = [NSString stringWithUTF8String:option_f];
//            if (f&&![f isEqualToString:@"(null)"]) {
//                [optionArray addObject:f];
//            }
            [dict setObject:optionArray forKey:@"options"];
            [dict setObject:[NSString stringWithUTF8String:answer] forKey:@"answer"];
            [dict setObject:[NSString stringWithUTF8String:analysis] forKey:@"analysis"];
            [dict setObject:[NSString stringWithUTF8String:parent_id] forKey:@"parent_id"];
            [dict setObject:[NSString stringWithUTF8String:user_answer] forKey:@"user_answer"];
            [dict setObject:[NSString stringWithFormat:@"%d",user_error_count] forKey:@"user_error_count"];
            [dict setObject:[NSString stringWithFormat:@"%d",user_right_count] forKey:@"user_right_count"];
            [dict setObject:[NSString stringWithFormat:@"%d",favourite_flag] forKey:@"favourite_flag"];
            [dict setObject:[NSString stringWithFormat:@"%d",user_test_sign] forKey:@"user_test_sign"];
            [dict setObject:[NSString stringWithFormat:@"%d",has_sub] forKey:@"has_sub"];
            [dict setObject:[NSString stringWithFormat:@"%d",difficult_id] forKey:@"difficult_id"];
            [dict setObject:[NSString stringWithUTF8String:difficult_name] forKey:@"difficult_name"];
            [dict setObject:[NSString stringWithFormat:@"%d",titile_id] forKey:@"titile_id"];
            [dict setObject:[NSString stringWithFormat:@"%d",qst_no] forKey:@"qst_no"];
            [dict setObject:[NSString stringWithFormat:@"%d",tq_id] forKey:@"tq_id"];
            [dict setObject:[NSString stringWithUTF8String:author_id] forKey:@"author_id"];
            
            //[array addObject:[NSString stringWithUTF8String:Id]];
            [dataArray addObject:dict];
		}
    }
    FMSCHBLog(@"dataArray-------->%@",dataArray);
    return dataArray;
}
//收藏的题目
+(NSMutableArray *)selectCollectionQuestionPaper_Id:(NSString *)paper_id {
    if (![Sqlite creatTestQuestionTable]) {
		return nil;
	}
    NSMutableArray *dataArray = [NSMutableArray arrayWithCapacity:1];
    NSString *memberId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
    NSString *selectSQL = [NSString stringWithFormat:@"select * from testQuestionTable where  paper_id = '%@' and favourite_flag = '1'and author_id = '%@'",paper_id,memberId];
	sqlite3_stmt *statement;
    if (sqlite3_prepare_v2(database, [selectSQL UTF8String], -1, &statement, nil) == SQLITE_OK) {
		while (sqlite3_step(statement) == SQLITE_ROW) {
            NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:1];
			int qstId = (int)sqlite3_column_int(statement, 0);
            const char *paper_id = (char*)sqlite3_column_text(statement, 1);
            int orderid = (int)sqlite3_column_int(statement, 2);
			int category_id = (int)sqlite3_column_int(statement, 3);
            const char *category_name = (char*)sqlite3_column_text(statement,4);
			int qtype = (int)sqlite3_column_int(statement, 5);
			const char *qtype_name = (char*)sqlite3_column_text(statement, 6);
			int score = (int )sqlite3_column_int(statement, 7);
			const char *content = (char*)sqlite3_column_text(statement, 8);
			const char *option_a = (char*)sqlite3_column_text(statement, 9);
//            const char *option_b = (char*)sqlite3_column_text(statement, 10);
//			const char *option_c = (char*)sqlite3_column_text(statement,11);
//			const char *option_d = (char*)sqlite3_column_text(statement, 12);
//			const char *option_e = (char*)sqlite3_column_text(statement,13);
//			const char *option_f = (char*)sqlite3_column_text(statement, 14);
			const char *answer = (char*)sqlite3_column_text(statement, 15);
			const char *analysis = (char*)sqlite3_column_text(statement, 16);
			const char * parent_id = (char*)sqlite3_column_text(statement, 17);
            const char *user_answer = (char*)sqlite3_column_text(statement, 18);
			int user_error_count = (int )sqlite3_column_int(statement, 19);
			int user_right_count = (int )sqlite3_column_int(statement, 20);
			int favourite_flag = (int )sqlite3_column_int(statement, 21);
			int user_test_sign = (int )sqlite3_column_int(statement, 22);
            int has_sub = (int )sqlite3_column_int(statement, 23);
			int difficult_id = (int )sqlite3_column_int(statement, 24);
			const char *difficult_name = (char*)sqlite3_column_text(statement, 25);
			int titile_id = (int )sqlite3_column_int(statement, 26);
			int qst_no = (int )sqlite3_column_int(statement, 27);
			int tq_id = (int )sqlite3_column_int(statement, 28);
			const char *author_id = (char*)sqlite3_column_text(statement, 29);
            const char *paper_title = (char*)sqlite3_column_text(statement, 30);
            const char *paper_score = (char*)sqlite3_column_text(statement, 31);
            const char *paper_time = (char*)sqlite3_column_text(statement, 32);
            const char *myqtype = (char*)sqlite3_column_text(statement, 33);
            const char *collection_finish_flag = (char*)sqlite3_column_text(statement, 34);
            const char *userAnswerImage = (char*)sqlite3_column_text(statement, 35);
            [dict setObject:[NSString stringWithFormat:@"%d",qstId] forKey:@"qstId"];
            [dict setObject:[NSString stringWithUTF8String:paper_id] forKey:@"paper_id"];
            [dict setObject:[NSString stringWithFormat:@"%d",orderid] forKey:@"orderid"];
            [dict setObject:[NSString stringWithFormat:@"%d",category_id] forKey:@"category_id"];
            [dict setObject:[NSString stringWithUTF8String:category_name] forKey:@"category_name"];
            [dict setObject:[NSString stringWithFormat:@"%d",qtype] forKey:@"qtype"];
            [dict setObject:[NSString stringWithUTF8String:qtype_name] forKey:@"paper_title"];
            [dict setObject:[NSString stringWithFormat:@"%d",score] forKey:@"score"];
            [dict setObject:[NSString stringWithUTF8String:content] forKey:@"content"];
//            NSMutableArray *optionArray = [NSMutableArray arrayWithCapacity:1];
            NSString *a = [NSString stringWithUTF8String:option_a];
            NSArray *array1 = [a componentsSeparatedByString:@"$"];
            NSMutableArray *optionArray = [NSMutableArray arrayWithArray:array1];
            [optionArray removeLastObject];
//            if (a&&![a isEqualToString:@"(null)"]) {
//                [optionArray addObject:a];
//            }
//            NSString *b = [NSString stringWithUTF8String:option_b];
//            if (b&&![b isEqualToString:@"(null)"]) {
//                [optionArray addObject:b];
//            }
//            NSString *c = [NSString stringWithUTF8String:option_c];
//            if (c&&![c isEqualToString:@"(null)"]) {
//                [optionArray addObject:c];
//            }
//            NSString *d = [NSString stringWithUTF8String:option_d];
//            if (d&&![d isEqualToString:@"(null)"]) {
//                [optionArray addObject:d];
//            }
//            NSString *e = [NSString stringWithUTF8String:option_e];
//            if (e&&![e isEqualToString:@"(null)"]) {
//                [optionArray addObject:e];
//            }
//            NSString *f = [NSString stringWithUTF8String:option_f];
//            if (f&&![f isEqualToString:@"(null)"]) {
//                [optionArray addObject:f];
//            }
            [dict setObject:optionArray forKey:@"options"];
            [dict setObject:[NSString stringWithUTF8String:answer] forKey:@"answer"];
            [dict setObject:[NSString stringWithUTF8String:analysis] forKey:@"analysis"];
            [dict setObject:[NSString stringWithUTF8String:parent_id] forKey:@"parent_id"];
            [dict setObject:[NSString stringWithUTF8String:user_answer] forKey:@"user_answer"];
            [dict setObject:[NSString stringWithFormat:@"%d",user_error_count] forKey:@"user_error_count"];
            [dict setObject:[NSString stringWithFormat:@"%d",user_right_count] forKey:@"user_right_count"];
            [dict setObject:[NSString stringWithFormat:@"%d",favourite_flag] forKey:@"favourite_flag"];
            [dict setObject:[NSString stringWithFormat:@"%d",user_test_sign] forKey:@"user_test_sign"];
            [dict setObject:[NSString stringWithFormat:@"%d",has_sub] forKey:@"has_sub"];
            [dict setObject:[NSString stringWithFormat:@"%d",difficult_id] forKey:@"difficult_id"];
            [dict setObject:[NSString stringWithUTF8String:difficult_name] forKey:@"difficult_name"];
            [dict setObject:[NSString stringWithFormat:@"%d",titile_id] forKey:@"titile_id"];
            [dict setObject:[NSString stringWithFormat:@"%d",qst_no] forKey:@"qst_no"];
            [dict setObject:[NSString stringWithFormat:@"%d",tq_id] forKey:@"tq_id"];
            [dict setObject:[NSString stringWithUTF8String:author_id] forKey:@"author_id"];
            
            
            [dict setObject:[NSString stringWithUTF8String:paper_title] forKey:@"paper_title"];
            [dict setObject:[NSString stringWithUTF8String:paper_score] forKey:@"paper_score"];
            [dict setObject:[NSString stringWithUTF8String:paper_time] forKey:@"paper_time"];
            [dict setObject:[NSString stringWithUTF8String:myqtype] forKey:@"myqtype"];
            [dict setObject:[NSString stringWithUTF8String:collection_finish_flag] forKey:@"collection_finish_flag"];
            [dict setObject:[NSString stringWithUTF8String:userAnswerImage] forKey:@"userAnswerImage"];
            //[array addObject:[NSString stringWithUTF8String:Id]];
            [dataArray addObject:dict];
		}
    }
    FMSCHBLog(@"dataArray-------->%@",dataArray);
    return dataArray;
}
//测试同步练习都收藏在creatTestQuestionTable收藏的个数
+(int)queryCollectionCount:(NSString*)paper_title  paper_Id:(NSString *)paper_id{
	if (![Sqlite creatTestQuestionTable]) {
		return 0;
	}
    NSString *memberId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
	NSString *selectSQL = [NSString stringWithFormat:@"select count(favourite_flag) from testQuestionTable where favourite_flag = '1' and paper_id = '%@' and author_id = '%@';",paper_id,memberId];
//    NSString *selectSQL = [NSString stringWithFormat:@"select * from questionTable"];
	sqlite3_stmt *statement;
	int sum = 0;
    if (sqlite3_prepare_v2(database, [selectSQL UTF8String], -1, &statement, nil) == SQLITE_OK) {
		while (sqlite3_step(statement) == SQLITE_ROW) {
			int count = sqlite3_column_int(statement, 0);
			sum =count;
			FMSCHBLog(@"all count :%d",sum);
		}
	}
	sqlite3_finalize(statement);
	sqlite3_close(database);
	return sum;
}
+(BOOL)deleteAllCollectionQuestion{
	BOOL result = NO;
	if ([Sqlite creatTestQuestionTable]) {
		NSString *author_id = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
		NSString *updateSQL = [NSString stringWithFormat:@"update testQuestionTable set favourite_flag = '0' where  author_id ='%@';",author_id];
		char *errorMsg;
		if (sqlite3_exec(database, [updateSQL UTF8String], NULL, NULL, &errorMsg) == SQLITE_OK) {
			sqlite3_free(errorMsg);
			result = YES;
		}
		else {
		}
		sqlite3_close(database);
	}
	return result;
}
+(BOOL)deleteQuestionIdAt:(NSString*)question_Id paperId:(NSString *)type qtype:(NSString *)qtype{
	BOOL result = NO;
	if ([Sqlite creatPaperTable]) {
		NSString *author_id = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
		NSString *updateSQL = [NSString stringWithFormat:@"delete from questionTable where author_id ='%@' and orderid = '%ld' and paper_id = '%ld';",author_id,(long)[question_Id integerValue],(long)[type integerValue]];
		char *errorMsg;
		if (sqlite3_exec(database, [updateSQL UTF8String], NULL, NULL, &errorMsg) == SQLITE_OK) {
			sqlite3_free(errorMsg);
			result = YES;
		}
		else {
		}
		sqlite3_close(database);
	}
	return result;
}
//测试试题
+(BOOL)creatTestQuestionTable {
	NSString *path = FMPAPERSAVE_PATH;//[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Paper"];
	NSString *file = [path stringByAppendingPathComponent:kDatabaseName];
	//open database if exist, or create database
	if (sqlite3_open([file UTF8String], &database)!= SQLITE_OK) {
		sqlite3_close(database);
		return NO;
	}
	//create table
	char *erorMsg;
	NSString *createTableSQL = @"create table if not exists testQuestionTable(qstId text,paper_id text,orderid integer,category_id integer,category_name text,qtype integer,qtype_name text,score integer,content text,option_a text,option_b text,option_c text,option_d text,option_e text,option_f text,answer text,analysis text,parent_id text,user_answer text,user_error_count integer,user_right_count integer,favourite_flag integer,user_test_sign integer,has_sub text,difficult_id integer,difficult_name text,title_id integer,qst_no integer,tq_id text,author_id text,paper_title text,paper_score text,paper_time text,myqtype text,collection_finish_flag text,userAnswerImage text,currentPage_flag integer);";
    
	if (sqlite3_exec(database, [createTableSQL UTF8String], NULL, NULL, &erorMsg) != SQLITE_OK) {
		sqlite3_close(database);
		sqlite3_free(erorMsg);
		return NO;
	}
	return YES;
}
+(BOOL)isExsit:(NSMutableDictionary *)dict{
    BOOL hasFind = NO;
    if (![Sqlite creatTestQuestionTable]) {
        return hasFind;
    }
    NSString *paper_id = [dict objectForKey:@"paper_id"];
    NSString *tq_id = [dict objectForKey:@"tq_id"];
    NSString *memberId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
    NSString *selectSQL = [NSString stringWithFormat:@"select * from testQuestionTable where paper_id = '%@' and author_id = '%@' and tq_id='%@';",paper_id,memberId,tq_id];
    sqlite3_stmt *statement;
    if (sqlite3_prepare_v2(database, [selectSQL UTF8String], -1, &statement, nil) == SQLITE_OK) {
        while (sqlite3_step(statement) == SQLITE_ROW) {
            hasFind = YES;
        }
    }
    
    sqlite3_finalize(statement);
    sqlite3_close(database);
    return hasFind;
}
+(BOOL)insertTestQuestion:(NSMutableDictionary *)dict{
    NSString *author_id = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
    NSString *qstId = [dict objectForKey:@"qstId"];
    NSString *paper_id = [dict objectForKey:@"paper_id"];
    NSString *orderid = [dict objectForKey:@"orderid"];
    NSString *category_id = [dict objectForKey:@"category_id"];
    NSString *category_name = [dict objectForKey:@"category_name"];
    NSString *qtype = [dict objectForKey:@"qtype"];
    NSString *qtype_name = [dict objectForKey:@"qtype_name"];
    NSString *score = [dict objectForKey:@"score"];
    NSString *content = [Sqlite replaceWidthStringOfQuestionContent:[dict objectForKey:@"content"]];
    NSArray *array = [dict objectForKey:@"options"];
    NSMutableString *optionsString = [NSMutableString stringWithString:@""];
    //        NSArray *keyArray = [NSArray arrayWithObjects:@"option_a",@"option_b",@"option_c",@"option_d",@"option_e",@"option_f",@"option_g",@"option_h",@"option_i",@"option_j",@"option_k", nil];
    for (NSString *op in array) {
        [optionsString appendFormat:@"%@$",[Sqlite replaceWidthStringOfQuestionContent:op]];
        //            [dict setObject:[array objectAtIndex:i] forKey:[keyArray objectAtIndex:i]];
    }
    //        NSString *option_a = [dict objectForKey:@"option_a"];
    NSString *option_a = optionsString;
    NSString *option_b = [dict objectForKey:@"option_b"];
    NSString *option_c = [dict objectForKey:@"option_c"];
    NSString *option_d = [dict objectForKey:@"option_d"];
    NSString *option_e = [dict objectForKey:@"option_e"];
    NSString *option_f = [dict objectForKey:@"option_f"];
    NSString *answer = [dict objectForKey:@"answer"];
    NSString *analysis1 = [dict objectForKey:@"analysis"];
    FMSCHBLog(@"analysis1------>%@",analysis1);
    NSMutableString *analysis = [NSMutableString stringWithString:analysis1];
    [analysis replaceOccurrencesOfString:@"'" withString:@"\"" options:NSLiteralSearch range:NSMakeRange(0, [analysis length])];//过滤分析中的'字符替换微"
    NSString *parent_id = [dict objectForKey:@"parent_id"];
    NSString *user_answer = [dict objectForKey:@"user_answer"];
    NSString *use_error_count = [dict objectForKey:@"user_error_count"];
    NSString *use_right_count = [dict objectForKey:@"user_right_count"];
    NSString *favourite_flag = [dict objectForKey:@"favourite_flag"];
    NSString *user_test_sign = [dict objectForKey:@"user_test_sign"];
    NSString *has_sub = [dict objectForKey:@"has_sub"];
    NSString *difficult_id = [dict objectForKey:@"difficult_id"];
    NSString *difficult_name = [dict objectForKey:@"difficult_name"];
    NSString *title_id = [dict objectForKey:@"title_id"];
    NSString *qst_no = [dict objectForKey:@"qst_no"];
    NSString *tq_id = [dict objectForKey:@"tq_id"];
    NSString *paper_title = [dict objectForKey:@"paper_title"];
    NSString *paper_score = [dict objectForKey:@"paper_score"];
    NSString *paper_time = [dict objectForKey:@"paper_time"];
    NSString *myqtype = [dict objectForKey:@"myqtype"];//自定义的题号类型，从1开始
    NSString *finish_flag = [dict objectForKey:@"collection_finish_flag"];//作业管理作业完成收藏标示
    NSString *userAnswerImage = [dict objectForKey:@"userAnswerImage"];//简答题的文字和图片的表示
    if (userAnswerImage==nil) {
        userAnswerImage = @"";
    }
    NSString *updateSQL = nil;
    if ([Sqlite isExsit:dict]) {
        updateSQL = [NSString stringWithFormat:@"update testQuestionTable set qstId='%@',orderid='%ld',category_id='%ld',category_name='%@',qtype='%ld',qtype_name='%@',score='%ld',content='%@',option_a='%@',option_b='%@',option_c='%@',option_d='%@',option_e='%@',option_f='%@',answer='%@',analysis='%@',parent_id='%@',user_error_count='%ld',user_right_count='%ld',favourite_flag='%ld',user_test_sign='%ld',has_sub='%@',difficult_id='%ld',difficult_name='%@',title_id='%ld',qst_no='%ld',author_id='%@',paper_title='%@',paper_score='%@',paper_time='%@',myqtype='%@',collection_finish_flag='%@',userAnswerImage='%@' where paper_id='%@' and tq_id='%@';",qstId,(long)[orderid integerValue],(long)[category_id integerValue],category_name,(long)[qtype integerValue],qtype_name,(long)[score integerValue],content,option_a,option_b,option_c,option_d,option_e,option_f,answer,analysis,parent_id,(long)[use_error_count integerValue],(long)[use_right_count integerValue],(long)[favourite_flag integerValue],(long)[user_test_sign integerValue],has_sub,(long)[difficult_id integerValue],difficult_name,(long)[title_id integerValue],(long)[qst_no integerValue],author_id,paper_title,paper_score,paper_time,myqtype,finish_flag,userAnswerImage,paper_id,tq_id];
    }else{
        updateSQL = [NSString stringWithFormat:@"insert or replace into testQuestionTable(qstId,paper_id,orderid,category_id,category_name,qtype,qtype_name,score,content,option_a,option_b,option_c,option_d,option_e,option_f,answer,analysis,parent_id,user_answer,user_error_count,user_right_count,favourite_flag,user_test_sign,has_sub,difficult_id,difficult_name,title_id,qst_no,tq_id,author_id,paper_title,paper_score,paper_time,myqtype,collection_finish_flag,userAnswerImage,currentPage_flag) values('%@','%@','%ld','%ld','%@','%ld','%@','%ld','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%ld','%ld','%ld','%ld','%@','%ld','%@','%ld','%ld','%@','%@','%@','%@','%@','%@','%@','%@','%d');",qstId,paper_id,(long)[orderid integerValue],(long)[category_id integerValue],category_name,(long)[qtype integerValue],qtype_name,(long)[score integerValue],content,option_a,option_b,option_c,option_d,option_e,option_f,answer,analysis,parent_id,user_answer,(long)[use_error_count integerValue],(long)[use_right_count integerValue],(long)[favourite_flag integerValue],(long)[user_test_sign integerValue],has_sub,(long)[difficult_id integerValue],difficult_name,(long)[title_id integerValue],(long)[qst_no integerValue],tq_id,author_id,paper_title,paper_score,paper_time,myqtype,finish_flag,userAnswerImage,0];
    }
    BOOL result = NO;
    if (![Sqlite creatTestQuestionTable]) {
        return result;
    }
    char *errorMsg;
    if (sqlite3_exec(database, [updateSQL UTF8String], NULL, NULL, &errorMsg) == SQLITE_OK) {
        sqlite3_free(errorMsg);
        result = YES;
    }else {
        FMSCHBLog(@"----errorMsg---%@",[NSString stringWithUTF8String:errorMsg]);
    }
    sqlite3_close(database);
	return result;
}
+(BOOL)deleteTestQuestionIdAt:(NSString*)paperId{
	BOOL result = NO;
	if ([Sqlite creatTestQuestionTable]) {
		NSString *author_id = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
		NSString *updateSQL = [NSString stringWithFormat:@"delete from testQuestionTable where author_id ='%@' and paper_id = '%ld';",author_id,(long)[paperId integerValue]];
		char *errorMsg;
		if (sqlite3_exec(database, [updateSQL UTF8String], NULL, NULL, &errorMsg) == SQLITE_OK) {
			sqlite3_free(errorMsg);
			result = YES;
		}
		else {
		}
		sqlite3_close(database);
	}
	return result;
}
+(NSMutableArray *)selectGetSubQuestion:(NSString *)parent_id paperId:(NSString *)paper_id{
    if (![Sqlite creatTestQuestionTable]) {
		return nil;
	}
    NSMutableArray  *dataArray = [NSMutableArray arrayWithCapacity:1];
    NSString *memberId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
	NSString *selectSQL = [NSString stringWithFormat:@"select * from testQuestionTable where paper_id = '%@' and author_id = '%@' and parent_id = '%@';",paper_id,memberId,parent_id];
    NSLog(@"查询子题目：%@",selectSQL);
    
	sqlite3_stmt *statement;
    NSInteger count = 1;
    if (sqlite3_prepare_v2(database, [selectSQL UTF8String], -1, &statement, nil) == SQLITE_OK) {
		while (sqlite3_step(statement) == SQLITE_ROW) {
            const char *content = (char*)sqlite3_column_text(statement, 8);
            const char *option_a = (char*)sqlite3_column_text(statement, 9);
            const char *answer = (char*)sqlite3_column_text(statement, 15);
            const char *analysis = (char*)sqlite3_column_text(statement, 16);
            const char *parent_id = (char*)sqlite3_column_text(statement, 17);
            const char *user_answer = (char*)sqlite3_column_text(statement, 18);
            const char *tq_id = (char*)sqlite3_column_text(statement, 28);
            NSString *questionContent = [NSString stringWithUTF8String:content];
            NSString *a = [NSString stringWithUTF8String:option_a];
            NSArray *array = [a componentsSeparatedByString:@"$"];
            NSMutableArray *optionArray = [NSMutableArray arrayWithArray:array];
            [optionArray removeLastObject];
            
            NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:1.0];
            [dict setObject:questionContent forKey:@"content"];
            [dict setObject:optionArray forKey:@"optionArray"];
            [dict setObject:[NSString stringWithUTF8String:answer] forKey:@"answer"];
            [dict setObject:[NSString stringWithUTF8String:analysis] forKey:@"analysis"];
            FMSCHBLog(@"----user_answer-------%s",user_answer);
            [dict setObject:[NSString stringWithUTF8String:user_answer] forKey:@"user_answer"];
            [dict setObject:[NSString stringWithFormat:@"%ld",(long)count] forKey:@"orderid"];
            [dict setObject:[NSString stringWithUTF8String:tq_id] forKey:@"tq_id"];
            [dict setObject:[NSString stringWithUTF8String:parent_id] forKey:@"parent_id"];
            [dict setObject:@"1" forKey:@"score"];
            [dict setObject:@"2" forKey:@"qtype"];
            [dataArray addObject:dict];
            count++;
		}
	}
    return dataArray;
}
//获取所有试题的数组
+(NSMutableArray *)selectTestQuestion:(NSString *)paper_id{
    if (![Sqlite creatTestQuestionTable]) {
		return nil;
	}
    NSMutableArray  *dataArray = [NSMutableArray arrayWithCapacity:1];

    NSString *memberId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
	NSString *selectSQL = [NSString stringWithFormat:@"select * from testQuestionTable where paper_id = '%@' and author_id = '%@' and has_sub!='(null)';",paper_id,memberId];
	sqlite3_stmt *statement;
	BOOL hasFind = NO;
	if (sqlite3_prepare_v2(database, [selectSQL UTF8String], -1, &statement, nil) == SQLITE_OK) {
		while (sqlite3_step(statement) == SQLITE_ROW) {
            NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:1];
            const char *qstId = (char*)sqlite3_column_text(statement, 0);
            const char * paper_id =(char*)sqlite3_column_text(statement, 1);
            int orderid = (int)sqlite3_column_int(statement, 2);
			int category_id = (int)sqlite3_column_int(statement, 3);
            const char *category_name = (char*)sqlite3_column_text(statement,4);
			int qtype = (int)sqlite3_column_int(statement, 5);
			const char *qtype_name = (char*)sqlite3_column_text(statement, 6);
			int score = (int )sqlite3_column_int(statement, 7);
			const char *content = (char*)sqlite3_column_text(statement, 8);
			const char *option_a = (char*)sqlite3_column_text(statement, 9);
//            const char *option_b = (char*)sqlite3_column_text(statement, 10);
//			const char *option_c = (char*)sqlite3_column_text(statement,11);
//			const char *option_d = (char*)sqlite3_column_text(statement, 12);
//			const char *option_e = (char*)sqlite3_column_text(statement,13);
//			const char *option_f = (char*)sqlite3_column_text(statement, 14);
			const char *answer = (char*)sqlite3_column_text(statement, 15);
			const char *analysis = (char*)sqlite3_column_text(statement, 16);
			const char * parent_id = (char*)sqlite3_column_text(statement, 17);
            const char *user_answer = (char*)sqlite3_column_text(statement, 18);
			int user_error_count = (int )sqlite3_column_int(statement, 19);
			int user_right_count = (int )sqlite3_column_int(statement, 20);
			int favourite_flag = (int )sqlite3_column_int(statement, 21);
			int user_test_sign = (int )sqlite3_column_int(statement, 22);
            const char * has_sub = (char* )sqlite3_column_text(statement, 23);
			int difficult_id = (int )sqlite3_column_int(statement, 24);
			const char *difficult_name = (char*)sqlite3_column_text(statement, 25);
			int titile_id = (int )sqlite3_column_int(statement, 26);
			int qst_no = (int )sqlite3_column_int(statement, 27);
			const char *tq_id = (char*)sqlite3_column_text(statement, 28);
			const char *author_id = (char*)sqlite3_column_text(statement, 29);
            const char *paper_title = (char*)sqlite3_column_text(statement, 30);
            const char *paper_score = (char*)sqlite3_column_text(statement, 31);
            const char *paper_time = (char*)sqlite3_column_text(statement, 32);
            const char *myqtype = (char*)sqlite3_column_text(statement, 33);
            const char *collection_finish_flag = (char*)sqlite3_column_text(statement, 34);
            const char *userAnswerImage = (char*)sqlite3_column_text(statement, 35);
            int currentPage_flag = (int)sqlite3_column_int(statement, 36);

            [dict setObject:[NSString stringWithUTF8String:qstId] forKey:@"qstId"];
            [dict setObject:[NSString stringWithUTF8String:paper_id] forKey:@"paper_id"];
            [dict setObject:[NSString stringWithFormat:@"%d",orderid] forKey:@"orderid"];
            [dict setObject:[NSString stringWithFormat:@"%d",category_id] forKey:@"category_id"];
            [dict setObject:[NSString stringWithUTF8String:category_name] forKey:@"category_name"];
            [dict setObject:[NSString stringWithFormat:@"%d",qtype] forKey:@"qtype"];
            [dict setObject:[NSString stringWithUTF8String:qtype_name] forKey:@"qtype_name"];
            [dict setObject:[NSString stringWithFormat:@"%d",score] forKey:@"score"];
            [dict setObject:[NSString stringWithUTF8String:content] forKey:@"content"];
//            NSMutableArray *optionArray = [NSMutableArray arrayWithCapacity:1];
            NSString *a = [NSString stringWithUTF8String:option_a];
            NSArray *array = [a componentsSeparatedByString:@"$"];
            NSMutableArray *optionArray = [NSMutableArray arrayWithArray:array];
            [optionArray removeLastObject];
//            if (a&&![a isEqualToString:@"(null)"]) {
//                [optionArray addObject:a];
//            }
//            NSString *b = [NSString stringWithUTF8String:option_b];
//            if (b&&![b isEqualToString:@"(null)"]) {
//                [optionArray addObject:b];
//            }
//            NSString *c = [NSString stringWithUTF8String:option_c];
//            if (c&&![c isEqualToString:@"(null)"]) {
//                [optionArray addObject:c];
//            }
//            NSString *d = [NSString stringWithUTF8String:option_d];
//            if (d&&![d isEqualToString:@"(null)"]) {
//                [optionArray addObject:d];
//            }
//            NSString *e = [NSString stringWithUTF8String:option_e];
//            if (e&&![e isEqualToString:@"(null)"]) {
//                [optionArray addObject:e];
//            }
//            NSString *f = [NSString stringWithUTF8String:option_f];
//            if (f&&![f isEqualToString:@"(null)"]) {
//                [optionArray addObject:f];
//            }
            [dict setObject:optionArray forKey:@"options"];
            [dict setObject:[NSString stringWithUTF8String:answer] forKey:@"answer"];
            [dict setObject:[NSString stringWithUTF8String:analysis] forKey:@"analysis"];
            [dict setObject:[NSString stringWithUTF8String:parent_id] forKey:@"parent_id"];
            [dict setObject:[NSString stringWithUTF8String:user_answer] forKey:@"user_answer"];
            [dict setObject:[NSString stringWithFormat:@"%d",user_error_count] forKey:@"user_error_count"];
            [dict setObject:[NSString stringWithFormat:@"%d",user_right_count] forKey:@"user_right_count"];
            [dict setObject:[NSString stringWithFormat:@"%d",favourite_flag] forKey:@"favourite_flag"];
            [dict setObject:[NSString stringWithFormat:@"%d",user_test_sign] forKey:@"user_test_sign"];
            [dict setObject:[NSString stringWithUTF8String:has_sub] forKey:@"has_sub"];
            [dict setObject:[NSString stringWithFormat:@"%d",difficult_id] forKey:@"difficult_id"];
            [dict setObject:[NSString stringWithUTF8String:difficult_name] forKey:@"difficult_name"];
            [dict setObject:[NSString stringWithFormat:@"%d",titile_id] forKey:@"titile_id"];
            [dict setObject:[NSString stringWithFormat:@"%d",qst_no] forKey:@"qst_no"];
            [dict setObject:[NSString stringWithUTF8String:tq_id] forKey:@"tq_id"];
            [dict setObject:[NSString stringWithUTF8String:author_id] forKey:@"author_id"];
            [dict setObject:[NSString stringWithUTF8String:paper_title] forKey:@"paper_title"];
            [dict setObject:[NSString stringWithUTF8String:paper_score] forKey:@"paper_score"];
            [dict setObject:[NSString stringWithUTF8String:paper_time] forKey:@"paper_time"];
            [dict setObject:[NSString stringWithUTF8String:myqtype] forKey:@"myqtype"];
            [dict setObject:[NSString stringWithUTF8String:collection_finish_flag] forKey:@"collection_finish_flag"];
            [dict setObject:[NSString stringWithUTF8String:userAnswerImage] forKey:@"userAnswerImage"];
            [dict setObject:[NSString stringWithFormat:@"%d",currentPage_flag] forKey:@"currentPage_flag"];
            
            if ([[NSString stringWithUTF8String:has_sub] isEqualToString:@"1"]) {
                NSMutableArray *array = [self selectGetSubQuestion:[NSString stringWithUTF8String:tq_id] paperId:[NSString stringWithUTF8String:paper_id]];
                [dict setObject:array forKey:@"has_sub_Array"];
                [dataArray addObject:dict];
            }else if ([[NSString stringWithUTF8String:has_sub] isEqualToString:@"0"]){
                [dataArray addObject:dict];
            }
			hasFind = YES;
		}
	}

	sqlite3_finalize(statement);
	sqlite3_close(database);
	return dataArray;
}
//获取已答试题
+(NSMutableArray *)selectTestQuestionWhenNOAnswer:(NSString *)paper_id{
    if (![Sqlite creatTestQuestionTable]) {
        return nil;
    }
    NSMutableArray  *dataArray = [NSMutableArray arrayWithCapacity:1];
    
    NSString *memberId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
    NSString *selectSQL = [NSString stringWithFormat:@"select * from testQuestionTable where user_answer!='' and paper_id = '%@' and author_id = '%@';",paper_id,memberId];
    sqlite3_stmt *statement;
    BOOL hasFind = NO;
    if (sqlite3_prepare_v2(database, [selectSQL UTF8String], -1, &statement, nil) == SQLITE_OK) {
        while (sqlite3_step(statement) == SQLITE_ROW) {
            NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:1];
            const char *qstId = (char*)sqlite3_column_text(statement, 0);
            const char * paper_id =(char*)sqlite3_column_text(statement, 1);
            int orderid = (int)sqlite3_column_int(statement, 2);
            int category_id = (int)sqlite3_column_int(statement, 3);
            const char *category_name = (char*)sqlite3_column_text(statement,4);
            int qtype = (int)sqlite3_column_int(statement, 5);
            const char *qtype_name = (char*)sqlite3_column_text(statement, 6);
            int score = (int )sqlite3_column_int(statement, 7);
            const char *content = (char*)sqlite3_column_text(statement, 8);
            const char *option_a = (char*)sqlite3_column_text(statement, 9);
            //            const char *option_b = (char*)sqlite3_column_text(statement, 10);
            //			const char *option_c = (char*)sqlite3_column_text(statement,11);
            //			const char *option_d = (char*)sqlite3_column_text(statement, 12);
            //			const char *option_e = (char*)sqlite3_column_text(statement,13);
            //			const char *option_f = (char*)sqlite3_column_text(statement, 14);
            const char *answer = (char*)sqlite3_column_text(statement, 15);
            const char *analysis = (char*)sqlite3_column_text(statement, 16);
            const char * parent_id = (char*)sqlite3_column_text(statement, 17);
            const char *user_answer = (char*)sqlite3_column_text(statement, 18);
            int user_error_count = (int )sqlite3_column_int(statement, 19);
            int user_right_count = (int )sqlite3_column_int(statement, 20);
            int favourite_flag = (int )sqlite3_column_int(statement, 21);
            int user_test_sign = (int )sqlite3_column_int(statement, 22);
            int has_sub = (int )sqlite3_column_int(statement, 23);
            int difficult_id = (int )sqlite3_column_int(statement, 24);
            const char *difficult_name = (char*)sqlite3_column_text(statement, 25);
            int titile_id = (int )sqlite3_column_int(statement, 26);
            int qst_no = (int )sqlite3_column_int(statement, 27);
            int tq_id = (int )sqlite3_column_int(statement, 28);
            const char *author_id = (char*)sqlite3_column_text(statement, 29);
            const char *paper_title = (char*)sqlite3_column_text(statement, 30);
            const char *paper_score = (char*)sqlite3_column_text(statement, 31);
            const char *paper_time = (char*)sqlite3_column_text(statement, 32);
            const char *myqtype = (char*)sqlite3_column_text(statement, 33);
            const char *collection_finish_flag = (char*)sqlite3_column_text(statement, 34);
            const char *userAnswerImage = (char*)sqlite3_column_text(statement, 35);

            [dict setObject:[NSString stringWithUTF8String:qstId] forKey:@"qstId"];
            [dict setObject:[NSString stringWithUTF8String:paper_id] forKey:@"paper_id"];
            [dict setObject:[NSString stringWithFormat:@"%d",orderid] forKey:@"orderid"];
            [dict setObject:[NSString stringWithFormat:@"%d",category_id] forKey:@"category_id"];
            [dict setObject:[NSString stringWithUTF8String:category_name] forKey:@"category_name"];
            [dict setObject:[NSString stringWithFormat:@"%d",qtype] forKey:@"qtype"];
            [dict setObject:[NSString stringWithUTF8String:qtype_name] forKey:@"qtype_name"];
            [dict setObject:[NSString stringWithFormat:@"%d",score] forKey:@"score"];
            [dict setObject:[NSString stringWithUTF8String:content] forKey:@"content"];
            //            NSMutableArray *optionArray = [NSMutableArray arrayWithCapacity:1];
            NSString *a = [NSString stringWithUTF8String:option_a];
            NSArray *array = [a componentsSeparatedByString:@"$"];
            NSMutableArray *optionArray = [NSMutableArray arrayWithArray:array];
            [optionArray removeLastObject];
            //            if (a&&![a isEqualToString:@"(null)"]) {
            //                [optionArray addObject:a];
            //            }
            //            NSString *b = [NSString stringWithUTF8String:option_b];
            //            if (b&&![b isEqualToString:@"(null)"]) {
            //                [optionArray addObject:b];
            //            }
            //            NSString *c = [NSString stringWithUTF8String:option_c];
            //            if (c&&![c isEqualToString:@"(null)"]) {
            //                [optionArray addObject:c];
            //            }
            //            NSString *d = [NSString stringWithUTF8String:option_d];
            //            if (d&&![d isEqualToString:@"(null)"]) {
            //                [optionArray addObject:d];
            //            }
            //            NSString *e = [NSString stringWithUTF8String:option_e];
            //            if (e&&![e isEqualToString:@"(null)"]) {
            //                [optionArray addObject:e];
            //            }
            //            NSString *f = [NSString stringWithUTF8String:option_f];
            //            if (f&&![f isEqualToString:@"(null)"]) {
            //                [optionArray addObject:f];
            //            }
            [dict setObject:optionArray forKey:@"options"];
            [dict setObject:[NSString stringWithUTF8String:answer] forKey:@"answer"];
            [dict setObject:[NSString stringWithUTF8String:analysis] forKey:@"analysis"];
            [dict setObject:[NSString stringWithUTF8String:parent_id] forKey:@"parent_id"];
            [dict setObject:[NSString stringWithUTF8String:user_answer] forKey:@"user_answer"];
            [dict setObject:[NSString stringWithFormat:@"%d",user_error_count] forKey:@"user_error_count"];
            [dict setObject:[NSString stringWithFormat:@"%d",user_right_count] forKey:@"user_right_count"];
            [dict setObject:[NSString stringWithFormat:@"%d",favourite_flag] forKey:@"favourite_flag"];
            [dict setObject:[NSString stringWithFormat:@"%d",user_test_sign] forKey:@"user_test_sign"];
            if (has_sub == 1) {
                NSMutableArray *array = [self selectGetSubQuestion:[NSString stringWithFormat:@"%d",tq_id] paperId:[NSString stringWithFormat:@"%s",paper_id]];
                NSArray *options = [NSArray arrayWithObjects:@"option_a",@"option_b",@"option_c",@"option_d",@"option_e",@"option_f",@"option_g",@"option_h",@"option_i", @"option_j",@"option_k",@"option_m",@"option_l",@"option_n",@"option_o",@"option_p",@"option_q",@"option_r",@"option_s",@"option_t",@"option_u",@"option_v",@"option_w",@"option_x",@"option_y",@"option_z",nil];
                for (NSInteger i=0; i<[array count]-1; i++) {
                    NSString *option = [array objectAtIndex:i];
                    [dict setObject:option forKey:[options objectAtIndex:i]];
                }
                NSDictionary *lastDict = [array lastObject];
                [dict setObject:[lastDict objectForKey:@"analysis"] forKey:@"analysis"];
            }
            [dict setObject:[NSString stringWithFormat:@"%d",has_sub] forKey:@"has_sub"];
            [dict setObject:[NSString stringWithFormat:@"%d",difficult_id] forKey:@"difficult_id"];
            [dict setObject:[NSString stringWithUTF8String:difficult_name] forKey:@"difficult_name"];
            [dict setObject:[NSString stringWithFormat:@"%d",titile_id] forKey:@"titile_id"];
            [dict setObject:[NSString stringWithFormat:@"%d",qst_no] forKey:@"qst_no"];
            [dict setObject:[NSString stringWithFormat:@"%d",tq_id] forKey:@"tq_id"];
            [dict setObject:[NSString stringWithUTF8String:author_id] forKey:@"author_id"];
            [dict setObject:[NSString stringWithUTF8String:paper_title] forKey:@"paper_title"];
            [dict setObject:[NSString stringWithUTF8String:paper_score] forKey:@"paper_score"];
            [dict setObject:[NSString stringWithUTF8String:paper_time] forKey:@"paper_time"];
            [dict setObject:[NSString stringWithUTF8String:myqtype] forKey:@"myqtype"];
            [dict setObject:[NSString stringWithUTF8String:collection_finish_flag] forKey:@"collection_finish_flag"];
            [dict setObject:[NSString stringWithUTF8String:userAnswerImage] forKey:@"userAnswerImage"];
            [dataArray addObject:dict];
            hasFind = YES;
        }
    }
    
    sqlite3_finalize(statement);
    sqlite3_close(database);
    return dataArray;
}
+(NSMutableArray *)selectUserAnswerTestQuestion:(NSString *)paper_id{
    if (![Sqlite creatTestQuestionTable]) {
		return nil;
	}
    NSMutableArray  *dataArray = [NSMutableArray arrayWithCapacity:1];
    
    NSString *memberId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
	NSString *selectSQL = [NSString stringWithFormat:@"select user_answer from testQuestionTable where paper_id = '%ld' and author_id = '%@';",(long)[paper_id integerValue],memberId];
	sqlite3_stmt *statement;
	BOOL hasFind = NO;
	if (sqlite3_prepare_v2(database, [selectSQL UTF8String], -1, &statement, nil) == SQLITE_OK) {
		while (sqlite3_step(statement) == SQLITE_ROW) {
            NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:1];
            const char *user_answer = (char*)sqlite3_column_text(statement, 0);
            [dict setObject:[NSString stringWithUTF8String:user_answer] forKey:@"user_answer"];
            [dataArray addObject:dict];
			hasFind = YES;
		}
	}
    
	sqlite3_finalize(statement);
	sqlite3_close(database);
	return dataArray;
}

#pragma mark - 查询答案
+(NSString *)selectUserAnswerWithKey: (NSString*)key
                             paperId:(NSString *)paper_id
                          questionId: (NSString*)qstId{
    if (![Sqlite creatTestQuestionTable]) {
        return nil;
    }
    NSString *answer = nil;
    
    NSString *memberId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
    NSString *selectSQL = [NSString stringWithFormat:@"select %@ from testQuestionTable where paper_id = '%ld' and author_id = '%@' and tq_id = '%ld';",key,
                           (long)[paper_id integerValue],
                           memberId,
                           (long)(long)[qstId integerValue]];
    
    NSLog(@"查询题目: %@",selectSQL);
    sqlite3_stmt *statement;
    BOOL hasFind = NO;
    if (sqlite3_prepare_v2(database, [selectSQL UTF8String], -1, &statement, nil) == SQLITE_OK) {
        while (sqlite3_step(statement) == SQLITE_ROW) {
            const char *user_answer = (char*)sqlite3_column_text(statement, 0);
            answer = [NSString stringWithUTF8String:user_answer];
            hasFind = YES;
        }
    }
    sqlite3_finalize(statement);
    sqlite3_close(database);
    return answer;
}


+(BOOL)updateUserAnswer:(NSString*)user_answer user_right_count:(NSString *)user_right_count  title_id:(NSString *)qstId paperId:(NSString *)paperId{
    BOOL isResult;
	if(![Sqlite creatTestQuestionTable]) {
        isResult = NO;
		return isResult;
	}
    NSString *author_id = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
    NSString *updateSQL = [NSString stringWithFormat:@"update testQuestionTable set user_answer = '%@' where tq_id='%ld' and paper_id = '%@' and author_id ='%@';", user_answer,(long)[qstId integerValue],paperId,author_id];
	char *errorMsg;
    NSLog(@"更新输入答案: %@",updateSQL);
	if (sqlite3_exec(database, [updateSQL UTF8String], NULL, NULL, &errorMsg) == SQLITE_OK) {
		sqlite3_free(errorMsg);
        isResult = YES;
	}
	else {
		FMSCHBLog(@"updateData fail error:%s",errorMsg);
        isResult = NO;
		return isResult;
	}
	sqlite3_close(database);
	return isResult;
}
//简单题中的不定向选择 20160113
+(BOOL)updateSimpleSingleUserAnswer:(NSString*)user_answer user_right_count:(NSString *)user_right_count  tqId:(NSString *)tqId paperId:(NSString *)paperId parentid:(NSString *)parentid{
    BOOL isResult;
    if(![Sqlite creatTestQuestionTable]) {
        isResult = NO;
        return isResult;
    }
    NSString *author_id = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
    NSString *updateSQL = [NSString stringWithFormat:@"update testQuestionTable set user_answer = '%@' where parent_id='%@' and tq_id='%@' and paper_id = '%@' and author_id ='%@';", user_answer,parentid,tqId,paperId,author_id];
    char *errorMsg;
    if (sqlite3_exec(database, [updateSQL UTF8String], NULL, NULL, &errorMsg) == SQLITE_OK) {
        sqlite3_free(errorMsg);
        isResult = YES;
    }
    else {
        FMSCHBLog(@"updateData fail error:%s",errorMsg);
        isResult = NO;
        return isResult;
    }
    sqlite3_close(database);
    return isResult;
}

+(BOOL)updateUserAnswer:(NSString*)user_answer paperId:(NSString *)paper_id title_id:(NSString *)title_id{
	if(![Sqlite creatTestQuestionTable]) {
		return NO;
	}
    //    NSData *nameData = [username dataUsingEncoding:NSUTF8StringEncoding];
    //    NSString *encodedNameStr = [nameData base64Encoding];
    //    NSData *pwdData = [password dataUsingEncoding:NSUTF8StringEncoding];
    //    NSString *encodedPwdStr = [pwdData base64Encoding];
    NSString *author_id = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
    NSString *updateSQL = [NSString stringWithFormat:@"update testQuestionTable set  user_answer = '%@' where  paper_id='%@' and tq_id='%ld' and author_id ='%@';", user_answer,paper_id,(long)[title_id integerValue],author_id];
	char *errorMsg;
	if (sqlite3_exec(database, [updateSQL UTF8String], NULL, NULL, &errorMsg) == SQLITE_OK) {
		sqlite3_free(errorMsg);
	}
	else {
		FMSCHBLog(@"updateData fail error:%s",errorMsg);
		return NO;
	}
	sqlite3_close(database);
	return YES;
}
+(BOOL)updateUserAnswerImage:(NSString*)userAnswerImage paperId:(NSString *)paper_id title_id:(NSString *)qstId{
    if(![Sqlite creatTestQuestionTable]) {
        return NO;
    }
    //    NSData *nameData = [username dataUsingEncoding:NSUTF8StringEncoding];
    //    NSString *encodedNameStr = [nameData base64Encoding];
    //    NSData *pwdData = [password dataUsingEncoding:NSUTF8StringEncoding];
    //    NSString *encodedPwdStr = [pwdData base64Encoding];
    NSString *author_id = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
    NSString *updateSQL = [NSString stringWithFormat:@"update testQuestionTable set  userAnswerImage = '%@' where  qstId='%ld' and paper_id='%@' and author_id ='%@';", userAnswerImage,(long)[qstId integerValue],paper_id,author_id];
    char *errorMsg;
    if (sqlite3_exec(database, [updateSQL UTF8String], NULL, NULL, &errorMsg) == SQLITE_OK) {
        sqlite3_free(errorMsg);
    }
    else {
        FMSCHBLog(@"updateData fail error:%s",errorMsg);
        return NO;
    }
    sqlite3_close(database);
    return YES;
}
+(BOOL)updateUserAnswer:(NSString*)user_answer paperId:(NSString *)paper_id{
    if(![Sqlite creatTestQuestionTable]) {
        return NO;
    }
    //    NSData *nameData = [username dataUsingEncoding:NSUTF8StringEncoding];
    //    NSString *encodedNameStr = [nameData base64Encoding];
    //    NSData *pwdData = [password dataUsingEncoding:NSUTF8StringEncoding];
    //    NSString *encodedPwdStr = [pwdData base64Encoding];
    NSString *author_id = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
    NSString *updateSQL = [NSString stringWithFormat:@"update testQuestionTable set user_answer = '%@' where  paper_id='%@' and author_id ='%@';", user_answer,paper_id,author_id];
    char *errorMsg;
    if (sqlite3_exec(database, [updateSQL UTF8String], NULL, NULL, &errorMsg) == SQLITE_OK) {
        sqlite3_free(errorMsg);
    }
    else {
        FMSCHBLog(@"updateData fail error:%s",errorMsg);
        return NO;
    }
    sqlite3_close(database);
    return YES;
}
//保存当前进度
+(BOOL)updateCurrentPageFlag:(NSInteger)currentPage paperId:(NSString *)paper_id{
    if(![Sqlite creatTestQuestionTable]) {
        return NO;
    }
    NSString *author_id = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
    NSString *updateSQL = [NSString stringWithFormat:@"update testQuestionTable set currentPage_flag = '%ld' where  paper_id='%@' and author_id ='%@';", currentPage,paper_id,author_id];
    char *errorMsg;
    if (sqlite3_exec(database, [updateSQL UTF8String], NULL, NULL, &errorMsg) == SQLITE_OK) {
        sqlite3_free(errorMsg);
    }
    else {
        FMSCHBLog(@"updateData fail error:%s",errorMsg);
        return NO;
    }
    sqlite3_close(database);
    return YES;
}
+(BOOL)updateUserAnswerImage:(NSString*)userAnswerImage paperId:(NSString *)paper_id{
    if(![Sqlite creatTestQuestionTable]) {
        return NO;
    }
    //    NSData *nameData = [username dataUsingEncoding:NSUTF8StringEncoding];
    //    NSString *encodedNameStr = [nameData base64Encoding];
    //    NSData *pwdData = [password dataUsingEncoding:NSUTF8StringEncoding];
    //    NSString *encodedPwdStr = [pwdData base64Encoding];
    NSString *author_id = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
    NSString *updateSQL = [NSString stringWithFormat:@"update testQuestionTable set userAnswerImage = '%@' where  paper_id='%@' and author_id ='%@';", userAnswerImage,paper_id,author_id];
    char *errorMsg;
    if (sqlite3_exec(database, [updateSQL UTF8String], NULL, NULL, &errorMsg) == SQLITE_OK) {
        sqlite3_free(errorMsg);
    }
    else {
        FMSCHBLog(@"updateData fail error:%s",errorMsg);
        return NO;
    }
    sqlite3_close(database);
    return YES;
}
+(NSMutableArray *)selectUserAnswerErrorTestQuestion:(NSString *)paper_id{
    if (![Sqlite creatTestQuestionTable]) {
        return nil;
    }
    NSMutableArray  *dataArray = [NSMutableArray arrayWithCapacity:1];
    
    NSString *memberId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
    NSString *selectSQL = [NSString stringWithFormat:@"select category_name from testQuestionTable where paper_id = '%ld'and user_error_count = '%d' and author_id = '%@';",(long)[paper_id integerValue],0,memberId];
    sqlite3_stmt *statement;
    BOOL hasFind = NO;
    if (sqlite3_prepare_v2(database, [selectSQL UTF8String], -1, &statement, nil) == SQLITE_OK) {
        while (sqlite3_step(statement) == SQLITE_ROW) {
            NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:1];
            const char *category_name = (char*)sqlite3_column_text(statement, 0);
            if ([[NSString stringWithUTF8String:category_name] isEqualToString:@"(null)"]) {//没有知识点不添加
                NSLog(@"category_name--->%@",[NSString stringWithUTF8String:category_name]);
            }else{
                [dict setObject:[NSString stringWithUTF8String:category_name]forKey:@"category_name"];
                if (![dataArray containsObject:dict]) {//去重
                    [dataArray addObject:dict];
                }
            }
            hasFind = YES;
        }
    }
    
    sqlite3_finalize(statement);
    sqlite3_close(database);
    return dataArray;
}
+(BOOL)deleteUserAnswerPaper{
	BOOL result = NO;
	if ([Sqlite creatTestQuestionTable]) {
//		NSString *userName = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
		NSString *updateSQL = [NSString stringWithFormat:@"delete from testQuestionTable "];
		char *errorMsg;
		if (sqlite3_exec(database, [updateSQL UTF8String], NULL, NULL, &errorMsg) == SQLITE_OK) {
			sqlite3_free(errorMsg);
			result = YES;
		}
		else {
		}
		sqlite3_close(database);
	}
	return result;
}
+(NSMutableArray *)selectSimpleQuestion:(NSString *)selectSQL{
    if (![Sqlite creatQuestionTable]) {
		return nil;
	}
    NSMutableArray *dataArray = [NSMutableArray arrayWithCapacity:1];
//    NSString *author_id = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
//	NSString *selectSQL = [NSString stringWithFormat:@"select user_answer from testQuestionTable where author_id ='%@' and paper_id = '%@';",author_id,paperId];
	sqlite3_stmt *statement;
	if (sqlite3_prepare_v2(database, [selectSQL UTF8String], -1, &statement, nil) == SQLITE_OK) {
		while (sqlite3_step(statement) == SQLITE_ROW) {
            NSMutableDictionary *dataDict = [NSMutableDictionary dictionaryWithCapacity:1];
             const char *user_answer = (char *)sqlite3_column_text(statement, 0);
            NSString *user_answer_ = [NSString stringWithUTF8String:user_answer];
            FMSCHBLog(@"user_answer ======%@",user_answer_);
            [dataDict setObject:user_answer_ forKey:@"user_answer"];
            [dataArray addObject:dataDict];
		}
	}
    //    [dataDict setObject:dataArray forKey:@"RightCount"];
	sqlite3_finalize(statement);
	sqlite3_close(database);
	return dataArray;
}
+(NSMutableDictionary *)selectQuestionRightCount{
    if (![Sqlite creatQuestionTable]) {
		return nil;
	}
    NSMutableDictionary *dataDict = [NSMutableDictionary dictionaryWithCapacity:1];
    NSString *author_id = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
	NSString *selectSQL = [NSString stringWithFormat:@"select user_right_count user_error_count from testQuestionTable where author_id ='%@' and user_right_count ='%d' and user_error_count = '%d';",author_id,1,0];
	sqlite3_stmt *statement;
        int sum = 0;
	if (sqlite3_prepare_v2(database, [selectSQL UTF8String], -1, &statement, nil) == SQLITE_OK) {
		while (sqlite3_step(statement) == SQLITE_ROW) {
           int  user_right_count = sqlite3_column_int(statement, 0);
            int  user_error_count = sqlite3_column_int(statement, 1);
            if (user_right_count == 1&&user_error_count == 0) {
                sum++;
            }
            
		}
	}

    NSString *Str = [NSString stringWithFormat:@"%d",sum];
    [dataDict setObject:Str forKey:@"sum"];
//    [dataDict setObject:dataArray forKey:@"RightCount"];
	sqlite3_finalize(statement);
	sqlite3_close(database);
	return dataDict;
}
+(BOOL)updateUserAnswer:(NSString*)updateSQL{
    if(![Sqlite creatTestQuestionTable]) {
        return NO;
    }
    //    NSData *nameData = [username dataUsingEncoding:NSUTF8StringEncoding];
    //    NSString *encodedNameStr = [nameData base64Encoding];
    //    NSData *pwdData = [password dataUsingEncoding:NSUTF8StringEncoding];
    //    NSString *encodedPwdStr = [pwdData base64Encoding];
//    NSString *author_id = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
//    NSString *updateSQL = [NSString stringWithFormat:@"update testQuestionTable set user_answer = '%@' where  paper_id='%@' and author_id ='%@';", user_answer,paper_id,author_id];
    char *errorMsg;
    if (sqlite3_exec(database, [updateSQL UTF8String], NULL, NULL, &errorMsg) == SQLITE_OK) {
        sqlite3_free(errorMsg);
    }
    else {
        FMSCHBLog(@"updateData fail error:%s",errorMsg);
        return NO;
    }
    sqlite3_close(database);
    return YES;
}
//确定题型数目
+(NSInteger )selectQuestionTypeCount:(NSString *)paperId myqtype:(NSString *)myqtype {
    if (![Sqlite creatTestQuestionTable]) {
        return 0;
    }
    NSString *author_id = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
	NSString *selectSQL = [NSString stringWithFormat:@"select count(*) from testQuestionTable where paper_id = '%@' and author_id = '%@' and myqtype= '%@';",paperId,author_id,myqtype];
    sqlite3_stmt *statement;
    int  count;
    if (sqlite3_prepare_v2(database, [selectSQL UTF8String], -1, &statement, nil) == SQLITE_OK) {
        while (sqlite3_step(statement) == SQLITE_ROW) {
            count = sqlite3_column_int(statement, 0);
        }
    }
    sqlite3_finalize(statement);
    sqlite3_close(database);
    return count;
}


#pragma mark - 替换题目/选项中 'width=100%'
+(NSString*)replaceWidthStringOfQuestionContent: (NSString*)qstContent{
    // 截取img width=""
    NSString *width = @"width=\"100%\"";
    if ([qstContent containsString:width]) {
        return [qstContent stringByReplacingOccurrencesOfString:width withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, qstContent.length-1)];
        NSLog(@"question content after replacing is: %@",qstContent);
    }
    return qstContent;
}

@end

